package com.example.hospital.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hospital.constant.UserConstants;
import com.example.hospital.dto.LoginForm;
import com.example.hospital.dto.PatientQuery;
import com.example.hospital.dto.Result;
import com.example.hospital.entity.Patient;
import com.example.hospital.mapper.PatientMapper;
import com.example.hospital.service.PatientService;
import com.example.hospital.util.JwtHelper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author Oreki
 * @description 针对表【patient】的数据库操作Service实现
 * @createDate 2024-12-27 16:53:20
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient>
        implements PatientService {

    @Override
    public Result<Map<String, String>> login(LoginForm loginForm) {
        if (loginForm == null) {
            return Result.fail("登录信息不能为空");
        }
        String phone = loginForm.getPhone();
        String password = loginForm.getPassword();
        if (StrUtil.isEmpty(phone) || StrUtil.isEmpty(password)) {
            return Result.fail("手机号或密码不能为空");
        }
        Patient patient = this.getPatientByPhoneNumber(phone);
        if (patient == null || !password.equals(patient.getPassword())) {
            return Result.fail("手机号或密码错误");
        }
        String token = JwtHelper.createToken(phone, UserConstants.Role.PATIENT);
        return Result.success(Map.of("token", token, "id", patient.getPatientId().toString(), "name", patient.getName()));
    }

    @Override
    public Patient getPatientByPhoneNumber(String phoneNumber) {
        if (StrUtil.isEmpty(phoneNumber)) {
            return null;
        }
        return this.lambdaQuery().eq(Patient::getPhoneNumber, phoneNumber).one();
    }

    @Override
    public Result register(Patient patient) {
        if (patient == null) {
            return Result.fail("注册信息不能为空");
        }
        String phoneNumber = patient.getPhoneNumber();
        String password = patient.getPassword();
        if (StrUtil.isEmpty(phoneNumber) || StrUtil.isEmpty(password)) {
            return Result.fail("手机号或密码不能为空");
        }
        if (!UserConstants.CHINESE_PHONE_PATTERN.matcher(phoneNumber).matches()) {
            return Result.fail("手机号格式错误");
        }
        Patient existPatient = this.getPatientByPhoneNumber(phoneNumber);
        if (existPatient != null) {
            return Result.fail("手机号已被注册");
        }
        boolean saved = this.save(patient);
        return saved ? Result.success("注册成功") : Result.fail("注册失败");
    }

    @Override
    public List<Patient> listPatients(PatientQuery patientQuery) {
        if (patientQuery == null) {
            return this.list();
        }
        String name = patientQuery.getName();
        String gender = patientQuery.getGender();
        String startDate = patientQuery.getStartDate();
        String endDate = patientQuery.getEndDate();
        String phoneNumber = patientQuery.getPhoneNumber();
        String address = patientQuery.getAddress();
        String email = patientQuery.getEmail();
        return this.lambdaQuery()
                .like(StrUtil.isNotEmpty(name), Patient::getName, name)
                .eq(StrUtil.isNotEmpty(gender), Patient::getGender, gender)
                .ge(StrUtil.isNotEmpty(startDate), Patient::getBirthDate, startDate)
                .le(StrUtil.isNotEmpty(endDate), Patient::getBirthDate, endDate)
                .eq(StrUtil.isNotEmpty(phoneNumber), Patient::getPhoneNumber, phoneNumber)
                .like(StrUtil.isNotEmpty(address), Patient::getAddress, address)
                .eq(StrUtil.isNotEmpty(email), Patient::getEmail, email)
                .list();
    }
}




